<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_huiqi365/model')->方法名();
*/
namespace jy_huiqi365;
defined('BY_JYA') or exit('error');
class api extends \plugin{
	public function checkinfo($params=array(),$noexi=false){
		$post=array(
			'realName'=>$params['realname'],
			'cardId'=>$params['cardnum'],
			'phoneNo'=>$params['mobile'],
			'cardPortrait'=>$params['cardimg1'],
			'cardNationalEmblem'=>$params['cardimg2'],
		);
		$orderid=$params['id']?:$this->uuid();
		$res=$this->request('userSign',$post,$orderid);
		if($res['businessStatus'] || !$res['explain']){
			exi('签约失败！'.$res['explain'],'error');
		}
        return $res;
    }
	public function getlog($mid=0,$setting=array(),$isexi=false){
		$setting=$setting?:plugin('jy_huiqi365')->plugin_setting('basic');
		$qianyue=pdo_get('jy_huiqi365_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if($qianyue['passed']!=1 && $isexi){
			exi('还未签约不能提现！','error');
		}
		return $qianyue;
	}
	public function tixian($params=array(),$noexi=false,$setting=array()){
		$setting=$setting?:plugin('jy_huiqi365')->plugin_setting('basic');
		$setting['pathno']=$setting['pathno']?:$setting['huiqi365_pathno'];
		$setting['mercode']=$setting['mercode']?:$setting['huiqi365_mercode'];
		$setting['prikey']=$setting['prikey']?:$setting['huiqi365_prikey'];
		$setting['pripubkey']=$setting['pripubkey']?:$setting['huiqi365_pripubkey'];
		$setting['mer_key']=$setting['mer_key']?:$setting['huiqi365_mer_key'];
		$qianyue=pdo_get('jy_huiqi365_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$params['mid']));
		if($qianyue['passed']!=1){
			exi('还未签约不能提现！','error');
		}
		$order=array(
			'orderId'=>$params['ordersn'],
			'realName'=>$params['realname']?:$qianyue['realname'],
			'cardId'=>$qianyue['cardnum'],
			'cardNo'=>$params['bankcard'],
			'phoneNo'=>$params['mobile']?:$qianyue['mobile'],
			'payAmount'=>$params['money'],
			'payMemo'=>$params['remark'],
		);
		$payInfos[]=$order;
		$post=array(
			'batchNo'=>$params['ordersn'],
			'pathNo'=>$setting['pathno'],
			'totalNum'=>1,
			'totalAmount'=>$params['money'],
			'payInfos'=>$payInfos,
		);
		$res=$this->request('batchPayment',$post,$params['id']);
		if($res['businessStatus'] || !$res['explain']){
			exi('提现失败！'.$res['explain'],'error');
		}
        return $res;
    }
	public function request($action,$bodyjson=array(),$orderid=0,$setting=array()){	
		$setting=$setting?:plugin('jy_huiqi365')->plugin_setting('basic');
		if(!$setting['isopen']){
		    if(!$noexi){
                exi('签约功能未启用！','error');
            }else{
		        return false;
            }
        }
		if(!$setting['prikey'] || !$setting['mer_key']){javascript:;
			exi('请联系管理员，设置基本参数！','error');
		}
		$mer_key =$setting['mer_key'];
		$prikey =$setting['prikey'];
		//组装head
		$head_data['function'] = $action;
		$head_data['merCode'] = $setting['mercode'];
		$head_data['version'] = "2.0";
		$head_data['reqTime'] = date("Y-m-d" . "\T" . "H:i:s" . "+08:00", time());
		$head_data = json_encode($head_data);
		$headUrlencode = urlencode($head_data);
		//组装body
		$bodyjson['pathNo'] = $setting['pathno'];
		$bodydata = json_encode($bodyjson, JSON_UNESCAPED_UNICODE);
		$bodyUrlencode = urlencode($bodydata);
		$aesBody = $this->encrypt($bodyUrlencode, $mer_key);
		$cipher = md5($headUrlencode . '#' . $aesBody . $mer_key);
		//私钥加签
		openssl_sign($aesBody, $signature, $prikey, OPENSSL_ALGO_SHA256);
		$encrypted = base64_encode($signature);//加密后的内容通常含有特殊字符，需要编码转换下，在网络间通过url传输时要注意base64编码是否是url安全的
		$jsonStr = json_encode(array('reqMsgId' => $orderid, 'head' => $headUrlencode, 'cipher' => $cipher, 'body' => $aesBody, 'sign' => $encrypted));
		//file_put_contents(ROOT_D.'/addons/jy_huiqi365/error_log.txt',$jsonStr."~~~~~~~~\n",FILE_APPEND);
		//echo "请求数据>>>>>>>" . $jsonStr;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL, "https://api.huiqi365.com/MerApi");
		curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
				'Content-Type:application/json;charset=utf-8',
				'Content-Length:' . strlen($jsonStr)
			)
		);
		$response = curl_exec($ch);
		curl_close($ch);
		$resJson = json_decode($response, true);
		$resJson['body'] = $this->decrypt($resJson['body'], $mer_key);
		$body = urldecode(mb_convert_encoding($resJson['body'], 'utf-8', 'gb2312'));
		$body=json_decode($body,true);
		//file_put_contents(ROOT_D.'/addons/jy_huiqi365/error_log.txt',ijson_encode($body)."~~~~~~~~\n",FILE_APPEND);
		if($body['code']){
			exi('错误码：'.$body['code'].$body['message'],'error');
		}
		return $body;
	}
	public function encrypt($data, $key)
    {
        $data = openssl_encrypt($data, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
        return base64_encode($data);
    }
    
    public function decrypt($data, $key)
    {
        $encrypted = base64_decode($data);
        return openssl_decrypt($encrypted, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
    }
    public function uuid(){
        $chars = md5(uniqid(mt_rand(), true));
        $uuid = substr($chars, 0, 8) . '-'
            . substr($chars, 8, 4) . '-'
            . substr($chars, 12, 4) . '-'
            . substr($chars, 16, 4) . '-'
            . substr($chars, 20, 12);
        return $uuid;
    }
}
?>